//定义请求根路径
const baseUrl = "http://localhost:8081";

// 同时发送异步代码的次数
let ajaxTimes = 0;


// 返回请求根路径
export const getBaseUrl = () => {
  return baseUrl;
}

// wxlogin封装
export const getWxLogin = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout: 5000,
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  });

}

/**
 * wx getUserProfile封装
 */
export const getUserProfile = () => {
  return new Promise((resolve, reject) => {
    wx.getUserProfile({
      desc: '获取用户信息',
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  });
}

// 获取地址的经纬度封装
export const requestLocation = (params) => {

  var start = new Date().getTime();
  console.log("ajaxTimes=" + ajaxTimes)

  ajaxTimes++;

  // 显示加载中 效果
  wx.showLoading({
    title: '加载中'
  });

  // 模拟网络延迟加载
  while (true)
    if (new Date().getTime() - start > 1 * 100) break;

  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      url: 'https://apis.map.qq.com/ws/geocoder/v1',
      key: params.key,
      address: params.address,
      success: (res) => {
        resolve(res.data.result)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {
        ajaxTimes--;
        if (ajaxTimes == 0) {
          //  关闭正在等待的图标
          wx.hideLoading();
        }
      }
    })
  });
}

// 获取周边地点封装
export const requestNearbySearch = (params) => {

  var start = new Date().getTime();
  console.log("ajaxTimes=" + ajaxTimes)

  ajaxTimes++;

  // 显示加载中 效果
  wx.showLoading({
    title: '加载中'
  });

  // 模拟网络延迟加载
  while (true)
    if (new Date().getTime() - start > 1 * 100) break;

  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      url: 'https://apis.map.qq.com/ws/place/v1/search',
      key: params.key,
      keyword: params.keyword,
      boundary: params.boundary,
      success: (res) => {
        resolve(res.data.data)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {
        ajaxTimes--;
        if (ajaxTimes == 0) {
          //  关闭正在等待的图标
          wx.hideLoading();
        }
      }
    })
  });
}


// 后端请求工具类
export const requestUtil = (params) => {

  //判断url中是否带有 /my/ 请求的是私有的路径 带上heander token
  let header = {
    ...params.header
  };
  if (params.url.includes("/rider/")) {
    //拼接header 带上token
    header["token"] = wx.getStorageSync('token')
  }

  var start = new Date().getTime();
  console.log("ajaxTimes=" + ajaxTimes)

  ajaxTimes++;


  // 显示加载中 效果
  wx.showLoading({
    title: '加载中'
  });


  // 模拟网络延迟加载
  while (true)
    if (new Date().getTime() - start > 1 * 100) break;



  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      header,
      url: baseUrl + params.url,
      success: (result) => {
        resolve(result.data)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {
        ajaxTimes--;
        if (ajaxTimes == 0) {
          //  关闭正在等待的图标
          wx.hideLoading();
        }
      }
    })
  });
}

// 后端文件上传类
export const upLoadFileUtil = (params) => {
  let header = {
    ...params.header
  };
  if (params.url.includes("/rider/")) {
    //拼接header 带上token
    header["token"] = wx.getStorageSync('token')
  }
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      ...params,
      header,
      filePath: params.filePath,
      name: params.name,
      url: baseUrl + params.url,
      success: (result) => {
        resolve(JSON.parse(result.data))
      },
      fail: (err) => {
        reject(err)
      }
    })
  });
}